home *** CD-ROM | disk | FTP | other *** search
/ GFX Sensations 1 / Graphic Sensations - Volume 1.iso / tools / amiga / 3d_tools / irit40s.lha / Irit / cagd_lib / bsp_wrt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-30  |  5.9 KB  |  201 lines

  1. /******************************************************************************
  2. * Bsp-Wrt.c - Bspline handling routines - write to file.              *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include <ctype.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "cagd_loc.h"
  11.  
  12. /******************************************************************************
  13. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  14. * otherwise.                                      *
  15. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  16. ******************************************************************************/
  17. int BspCrvWriteToFile(CagdCrvStruct *Crvs, char *FileName, int Indent,
  18.                          char *Comment, char **ErrStr)
  19. {
  20.     int i;
  21.     FILE *f;
  22.  
  23.     if ((f = fopen(FileName, "w")) == NULL) {
  24.     *ErrStr = "Fail to open file";
  25.     return FALSE;
  26.     }
  27.     i = BspCrvWriteToFile2(Crvs, f, Indent, Comment, ErrStr);
  28.  
  29.     fclose(f);
  30.  
  31.     return i;
  32. }
  33.  
  34. /******************************************************************************
  35. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  36. * otherwise. The file is not closed.                          *
  37. * If Comment is NULL, no comment is printed, if "" only internal coment.      *
  38. ******************************************************************************/
  39. int BspCrvWriteToFile2(CagdCrvStruct *Crvs, FILE *f, int Indent, char *Comment,
  40.                                 char **ErrStr)
  41. {
  42.     int i, j, Len, MaxCoord;
  43.  
  44.     if (Comment != NULL) {
  45.     _CagdFprintf(f, Indent, "#\n");
  46.     _CagdFprintf(f, Indent, "# cagd_lib - bspline curve(s) dump.\n");
  47.     _CagdFprintf(f, Indent, "#\n");
  48.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  49.     _CagdFprintf(f, Indent, "#\n");
  50.     }
  51.  
  52.     *ErrStr = NULL;
  53.  
  54.     while (Crvs) {
  55.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crvs -> PType);
  56.  
  57.     if (Crvs -> GType != CAGD_CBSPLINE_TYPE) {
  58.         *ErrStr = "Given curve(s) is (are) not Bspline curve(s)";
  59.         break;
  60.     }
  61.     _CagdFprintf(f, Indent, "[CURVE BSPLINE %d %d %c%c\n",
  62.         Crvs -> Length, Crvs -> Order,
  63.         CAGD_IS_RATIONAL_PT(Crvs -> PType) ? 'P' : 'E',
  64.         MaxCoord + '0');
  65.     Indent += 4;
  66.  
  67.     /* Put out the knot vectors: */
  68.     _CagdFprintf(f, Indent, "[KV");
  69.     Len = Crvs -> Order + Crvs -> Length;
  70.     for (i = 0; i < Len; i++) {
  71.         _CagdFprintf(f, 0, " %s", _CagdReal2Str(Crvs -> KnotVector[i]));
  72.         if (i && i % 8 == 0) {
  73.         _CagdFprintf(f, 0, "\n");
  74.         _CagdFprintf(f, Indent + 3, "");
  75.         }
  76.     }
  77.     _CagdFprintf(f, 0, "]\n");
  78.  
  79.     /* Put out the control polygon. */
  80.     for (i = 0; i < Crvs -> Length; i++) {
  81.         _CagdFprintf(f, Indent, "[");
  82.         if (CAGD_IS_RATIONAL_PT(Crvs -> PType))
  83.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Crvs -> Points[0][i]));
  84.         for (j = 1; j <= MaxCoord; j++) {
  85.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Crvs -> Points[j][i]));
  86.         if (j < MaxCoord)
  87.             _CagdFprintf(f, 0, " ");
  88.         }
  89.         _CagdFprintf(f, 0, "]\n");
  90.     }
  91.  
  92.     Indent -= 4;
  93.     _CagdFprintf(f, Indent, "]\n\n");
  94.  
  95.     Crvs = Crvs -> Pnext;
  96.     }
  97.  
  98.     return *ErrStr == NULL;
  99. }
  100.  
  101. /******************************************************************************
  102. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  103. * otherwise.                                      *
  104. ******************************************************************************/
  105. int BspSrfWriteToFile(CagdSrfStruct *Srfs, char *FileName, int Indent,
  106.                          char *Comment,    char **ErrStr)
  107. {
  108.     int i;
  109.     FILE *f;
  110.  
  111.     if ((f = fopen(FileName, "w")) == NULL) {
  112.     *ErrStr = "Fail to open file";
  113.     return FALSE;
  114.     }
  115.     i = BspSrfWriteToFile2(Srfs, f, Indent, Comment, ErrStr);
  116.  
  117.     fclose(f);
  118.  
  119.     return i;
  120. }
  121.  
  122. /******************************************************************************
  123. * Writes a bspline curve(s) list into file. Returns TRUE if succesful, FALSE  *
  124. * otherwise. The file is not closed.                          *
  125. ******************************************************************************/
  126. int BspSrfWriteToFile2(CagdSrfStruct *Srfs, FILE *f, int Indent, char *Comment,
  127.                                 char **ErrStr)
  128. {
  129.     int i, j, Len, MaxCoord;
  130.     CagdRType *KnotVector;
  131.  
  132.     if (Comment != NULL) {
  133.     _CagdFprintf(f, Indent, "#\n");
  134.     _CagdFprintf(f, Indent, "# cagd_lib - bspline Srf(s) dump.\n");
  135.     _CagdFprintf(f, Indent, "#\n");
  136.     _CagdFprintf(f, Indent, "# %s\n", Comment);
  137.     _CagdFprintf(f, Indent, "#\n");
  138.     }
  139.  
  140.     *ErrStr = NULL;
  141.  
  142.     while (Srfs) {
  143.     MaxCoord = CAGD_NUM_OF_PT_COORD(Srfs -> PType);
  144.  
  145.     if (Srfs -> GType != CAGD_SBSPLINE_TYPE) {
  146.         *ErrStr = "Given surface(s) is (are) not bspline surface(s)";
  147.         break;
  148.     }
  149.     _CagdFprintf(f, Indent, "[SURFACE BSPLINE %d %d %d %d %c%c\n",
  150.         Srfs -> ULength, Srfs -> VLength,
  151.         Srfs -> UOrder, Srfs -> VOrder,
  152.         CAGD_IS_RATIONAL_PT(Srfs -> PType) ? 'P' : 'E',
  153.         MaxCoord + '0');
  154.     Indent += 4;
  155.  
  156.     /* Put out the knot vectors: */
  157.     for (i = 0; i < 2; i++) {
  158.         if (i == 0) {
  159.         KnotVector = Srfs -> UKnotVector;
  160.         Len = Srfs -> ULength + Srfs -> UOrder;
  161.         }
  162.         else {
  163.         KnotVector = Srfs -> VKnotVector;
  164.         Len = Srfs -> VLength + Srfs -> VOrder;
  165.         }
  166.  
  167.         _CagdFprintf(f, Indent, "[KV");
  168.         for (j = 0; j < Len; j++) {
  169.         _CagdFprintf(f, 0, " %s", _CagdReal2Str(KnotVector[j]));
  170.         if (j && j % 8 == 0) {
  171.             _CagdFprintf(f, 0, "\n\t\t");
  172.             _CagdFprintf(f, Indent + 3, "");
  173.         }
  174.         }
  175.         _CagdFprintf(f, 0, "]\n");
  176.     }
  177.  
  178.     /* Put out the control mesh. */
  179.     for (i = 0; i < Srfs -> VLength * Srfs -> ULength; i++) {
  180.         if (i && i % Srfs -> ULength == 0)
  181.         _CagdFprintf(f, 0, "\n");    /* Put empty lines between raws. */
  182.  
  183.         _CagdFprintf(f, Indent, "[");
  184.         if (CAGD_IS_RATIONAL_PT(Srfs -> PType))
  185.         _CagdFprintf(f, 0, "%s ", _CagdReal2Str(Srfs -> Points[0][i]));
  186.         for (j = 1; j <= MaxCoord; j++) {
  187.         _CagdFprintf(f, 0, "%s", _CagdReal2Str(Srfs -> Points[j][i]));
  188.         if (j < MaxCoord) _CagdFprintf(f, 0, " ");
  189.         }
  190.         _CagdFprintf(f, 0, "]\n");
  191.     }
  192.  
  193.     Indent -= 4;
  194.     _CagdFprintf(f, Indent, "]\n\n");
  195.  
  196.     Srfs = Srfs -> Pnext;
  197.     }
  198.  
  199.     return *ErrStr == NULL;
  200. }
  201.